package cn.anecansaitin.firecrafting.common.block;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.BaseFireBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor;
import org.jetbrains.annotations.NotNull;

import java.util.Random;
import java.util.function.Predicate;

//自定义火焰类
public class CustomFire extends BaseFireBlock {
    public final Predicate<BlockState> canBurn;

    //通过canBurn传入能让方块持续燃烧的条件
    public CustomFire(float damage, Predicate<BlockState> canBurn) {
        super(BlockBehaviour.Properties
                .of(Material.FIRE, MaterialColor.FIRE)
                .noCollission()
                .instabreak()
                .lightLevel((s) -> 15)
                .sound(SoundType.WOOL)
                .randomTicks(), damage);
        this.canBurn = canBurn;
    }

    //判断方块是否能持续燃烧
    @Override
    protected boolean canBurn(@NotNull BlockState state) {
        return canBurn.test(state);
    }

    @Override
    public BlockState updateShape(BlockState state, Direction face, BlockState facingState, LevelAccessor world, BlockPos currentPos, BlockPos facingPos) {
        if (face != Direction.DOWN) {
            return state;
        }

        return canBurn(state) ? this.defaultBlockState() : Blocks.AIR.defaultBlockState();
    }

    @Override
    public void tick(BlockState state, ServerLevel world, BlockPos pos, Random random) {
        if (!canBurn(world.getBlockState(pos.below())) && random.nextInt(10) < 3) {
            world.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState());
        }
    }
}
